package com.sczmgk.hotline.jpush;

import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.sczmgk.frame.http.Tools;
import com.sczmgk.hotline.httpUtils.MyHttp;
import com.sczmgk.hotline.utils.FileUtil;
import com.sczmgk.hotline.utils.MyLog;

import java.io.File;
import java.util.HashMap;
import java.util.List;

import cn.jiguang.share.android.api.JShareInterface;
import cn.jiguang.share.android.api.PlatActionListener;
import cn.jiguang.share.android.api.Platform;
import cn.jiguang.share.android.api.ShareParams;
import cn.jiguang.share.qqmodel.QQ;
import cn.jiguang.share.qqmodel.QZone;
import cn.jiguang.share.wechat.Wechat;
import cn.jiguang.share.weibo.SinaWeibo;

/**
 * Created by Administrator on 2017/7/1.
 */

public class JShareUtils {

    private static final String TAG = JShareUtils.class.getSimpleName();

    public static void getSharePlatformList(){
        List<String> platformList = JShareInterface.getPlatformList();
        for (int i = 0; i < platformList.size(); i++) {

            MyLog.d(TAG,"分享的平台："+platformList.get(i));
        }
        boolean clientValid = JShareInterface.isClientValid(Wechat.Name);
        MyLog.i(TAG,"wechat : "+clientValid);
        boolean sina = JShareInterface.isClientValid(SinaWeibo.Name);
        MyLog.i(TAG,"sina : "+sina);
        boolean qq = JShareInterface.isClientValid(QQ.Name);
        MyLog.i(TAG,"qq : "+qq);
        boolean qzone = JShareInterface.isClientValid(QZone.Name);
        MyLog.i(TAG,"qzone : "+qzone);
    }

    private static JShareUtils jShareUtils;
    private JShareUtils(){

    }
    public static JShareUtils getInstance(){
        if(jShareUtils == null){
            jShareUtils = new JShareUtils();
        }
        return  jShareUtils;
    }
    public  ShareCallback shareCallback;
    public interface ShareCallback{
        void onComplete(Platform platform, int i, HashMap<String, Object> hashMap);
        void onError(Platform platform, int i, int i1, Throwable throwable);
        void onCancel(Platform platform, int i);
    }


    public  void shareTest_wechat() {

    }
    public  void shareTest_qq() {

    }


    /**
     * 将网页链接分享到qq空间
     */
    public  void shareByWebPage(final String platform, MyShareParms myShareParms, ShareCallback shareCallback){
        this.shareCallback = shareCallback;



        final ShareParams shareParams = new ShareParams();
        shareParams.setShareType(Platform.SHARE_WEBPAGE);
        shareParams.setTitle(myShareParms.getTitle());
        shareParams.setUrl(myShareParms.getUrl());
        shareParams.setText(myShareParms.getText());

         if(platform.equals(Wechat.Name)){
             //1.存图片
             //2.设置文件路径或 bitMap
             if(!Tools.isEmpty(myShareParms.getImageUrl())){
                 MyHttp.downloadFile(myShareParms.getImageUrl(), FileUtil.getDownPicPath(), new RequestCallBack<File>() {
                     @Override
                     public void onSuccess(ResponseInfo<File> responseInfo) {
                         shareParams.setImagePath(FileUtil.getDownPicPath());
                         JShareInterface.share(platform,shareParams,platActionListener);
                     }

                     @Override
                     public void onFailure(HttpException error, String msg) {
                         JShareInterface.share(platform,shareParams,platActionListener);
                     }
                 });
             }else{
                 JShareInterface.share(platform,shareParams,platActionListener);
             }


         }else{
//             shareParams.setImageUrl(myShareParms.getImageUrl());
             JShareInterface.share(platform,shareParams,platActionListener);
         }


    }


     public  PlatActionListener platActionListener = new PlatActionListener() {
        @Override
        public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {
            MyLog.d(TAG,"share complete --   platform : "+platform+"  code: "+i);
            if(shareCallback != null){
                shareCallback.onComplete(platform,i,hashMap);
            }

        }

        @Override
        public void onError(Platform platform, int i, int i1, Throwable throwable) {
            MyLog.e(TAG,"share error   --- platform :"+platform+"   code1: "+ i +"   code2: "+i1 );

            if(shareCallback != null){
               shareCallback. onError(platform,i,i1,throwable);
            }
        }

        @Override
        public void onCancel(Platform platform, int i) {
            MyLog.e(TAG,"share cancel --   code :"+i);

            if(shareCallback != null){
                shareCallback.onCancel(platform,i);
            }
        }
    };

}
